package com.chzu.controller;

import com.chzu.config.security.contents.SecurityContents;
import com.chzu.mapper.SysUserMapper;
import com.chzu.pojo.SysUser;
import com.chzu.pojo.vo.LoginVo;
import com.chzu.result.JsonResult;
import com.chzu.service.SysUserService;
import com.chzu.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;

/**
 * @Author lz
 * @Version 1.0
 * @Company 滁州大健康与养老产业研究院
 * @ClassName LoginController
 * @Description 登录、退出、获取当前信息等
 * @Date 2022/01/27 14:07
 */
@RestController
@Slf4j
@RequestMapping("/user")
@Api(tags = "后台用户登录接口")
public class LoginController {

    @Resource
    private SysUserService sysUserService;

    @ApiOperation(value = "登录接口")
    @PostMapping("/login")
    public JsonResult<Object> login(@RequestBody LoginVo loginVo) {
        return sysUserService.login(loginVo);
    }

    @ApiOperation(value = "获取用户基本信息")
    @GetMapping("/getInfo")
    public JsonResult<Object> getUserInfo() {
        return JsonResult.ok( SecurityUtil.getUserInfo());
    }

    @ApiOperation(value = "退出登录")
    @GetMapping("/logout")
    public JsonResult<String> logout() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            SecurityContextHolder.getContext().setAuthentication(null);
        }
        return JsonResult.ok("退出成功");
    }

}
